package com.example.sources.util;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/9/18.
 */
public class ClassFileUtil {

    private List<Class> classList =new ArrayList<>();
    public  void setClassList(String dir) throws ClassNotFoundException {
        List<Class> list = new ArrayList<Class>();
        String newDir = dir.replace(".","/");
        String abSolutePath = getSrcPath() + newDir;
        String ss = abSolutePath.substring(6, abSolutePath.length());
        abSolutePath = ss.replace("/", File.separator);
        File dirFile = new File(abSolutePath);
        File[] files = dirFile.listFiles();
        Class<?> cla = null;
        for (File file:files){
            if (file.isDirectory()){
                if (dir.equals("")){
                    getClassList(file.getName());
                }else {
                    String child = dir + "."+file.getName();
                    getClassList(child);
                }

            }else {
                if (file.getName().split("\\.")[1].equals("class")){
                cla = Class.forName(dir+"."+file.getName().split("\\.")[0]);
                list.add(cla);
                }
            }
        }
        classList.addAll(list);

    }

    public   String getSrcPath(){
        String path="";
        try {
            path =Thread.currentThread().getContextClassLoader().getResource("")+"";  //获取src路径

        } catch (Exception e) {
            e.printStackTrace();
        }
        return path;
    }

    public  List<Class> getClassList(String dir){

        try {
            setClassList(dir);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return classList;
    }

}
